<?php

namespace app\company\model;

use app\common\enum\order\OrderStatus;
use app\common\model\BaseModel;
use app\common\model\Company;
use app\common\model\Dealer;
use app\common\model\DeliveryLog;
use app\common\model\Driver;
use app\common\model\ReceiveLog;
use app\common\model\Salesman;
use app\common\model\Store;
use app\common\enum\order\PayStatus;
use app\common\enum\order\ReceiveType;
use app\mini\model\ChangeCompanyOrderLog;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;

class CompanyOrder extends BaseModel
{
    public function detail(): HasMany
    {
        return $this->hasMany(CompanyOrderDetail::class, 'company_order_id', 'id');
    }

    public function orders(): HasMany
    {
        return $this->hasMany(CompanyOrder::class, 'pid', 'id');
    }

    public function detailAll(): HasMany
    {
        return $this->hasMany(CompanyOrderDetail::class, 'company_order_pid', 'id');
    }

    public function store(): BelongsTo
    {
        return $this->belongsTo(Store::class,'store_id','id');
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class,'company_id','id');
    }

    public function salesman(): BelongsTo
    {
        return $this->belongsTo(Salesman::class,'salesman_id','id');
    }

    public function dealer(): BelongsTo
    {
        return $this->belongsTo(Dealer::class,'dealer_id','id');
    }

    public function driver(): BelongsTo
    {
        return $this->belongsTo(Driver::class,'driver_id','id');
    }

    public function assistDriver(): BelongsTo
    {
        return $this->belongsTo(Driver::class,'assist_driver_id','id');
    }

    public function deliveryLog(): HasOne
    {
        return $this->hasOne(DeliveryLog::class,'company_order_id','id');
    }

    public function receiveLog()
    {
            return $this->hasOne(ReceiveLog::class,'company_order_id','id');
    }

    public function changeCompanyOrder(): HasOne
    {
        return $this->hasOne(ChangeCompanyOrderLog::class,'company_order_pid','id');
    }

    public function getPayStatusTextAttr($value,$data): string
    {
        $status = [PayStatus::SETTLED=>'已结算',PayStatus::NOT_SETTLED=>'未结算'];
        return $status[$data['pay_status']] ?? '无';
    }

    public function getReceiveTypeTextAttr($value,$data): string
    {
        $status = [ReceiveType::NOW=>'现结',ReceiveType::ZQ=>'账期',ReceiveType::YC=>'预存款'];
        return $status[$data['receive_type']] ?? '无';
    }

    public function getOrderStatusTextAttr($value,$data): string
    {
        $status = [OrderStatus::BEING=>'进行中',OrderStatus::FINISH=>'已完成',OrderStatus::CANCEL=>'已取消'];
        if($data['order_status'] == OrderStatus::BEING && $data['driver_id'] != 0){
            return '已通过,配送中';
        }
        return $status[$data['order_status']] ?? '无';
    }

    public function getCanCancelAttr($value,$data): string
    {
        return $data['driver_id'] == 0 ? ($data['order_status'] == 3 ? 2 : 1) : 2;
    }
}